import "package:get/get.dart";

import '../../../../apis/elevator.dart';
import '../../../../components/status/toast.dart';
import '../../../../controller/gather_ctrl.dart';
import '../../../../controller/network/bridge.dart';
import '../../../../controller/network/network_ctrl.dart';
import '../../../../model/bean/lift_info_bean.dart';

class LiftInfoCtrl extends GatherCtrl {
  // 查看网络状态
  final _nc = Get.find<NetworkCtrl>();

  // 组件是否初始化完成
  RxBool isInitComplete = false.obs;

  // 标记符号（用于检查请求完成后无数据组件与数据组件之的切换）
  final MARKSYMBOL = false.obs;

  // 请求参数
  final Map<String, dynamic> params = {"id": ""};

  // 页面数据
  final pageData = Rx<LiftInfoBean>();

  @override
  void onInit() {
    // 创建网络变化观察
    Bridget.createNetworkCtrl(this);

    // 赋值请求参数
    params["id"] = Get.arguments["liftId"];

    // 请求数据
    readPageLiftInfo();
  }

  @override
  void onClose() {
    // 移除观察
    Bridget.delNetworkCtrl(this);
  }

  @override
  void modify() {
    // 网络恢复正常时执行当前函数
    if (_nc.networkStatus.value) readPageLiftInfo(isToast: true);
  }

  /// 读取界面数据
  void readPageLiftInfo({bool isToast = false}) async {
    // 开启轻提示
    if (isToast) Toast.loading(title: '正在读取');

    try {
      LiftInfoBean result = await Elevator.queryLiftInfo(params);

      if (result != null) {
        pageData.value = result;
        MARKSYMBOL.value = true;
      }
    } catch (e) {}

    // 开启轻提示
    if (isToast) Toast.closeToast();

    // 标记加载完成
    isInitComplete.value = true;
  }
}
